package models.my;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Table;

import models.user.UserInfo;

import play.Logger;
import play.db.ebean.Model;
import tyrex.services.UUID;
import utils.MengException;
//14
@Entity
@Table(name="my_comment")
public class MyComment extends Model{

	private static final long serialVersionUID = 1L;
	
	public long id;
	public String username;
	public String posts_code;    //帖子的编号
	public String code = UUID.create();
	public String comment;
	public String comment_code;  //评论的父评论
	
	public static MyComment getInstances(){
		return new MyComment();
	}
	
	public static Model.Finder<Long, MyComment> find = new Model.Finder<Long, MyComment>(Long.class, MyComment.class);
	
	/**
	 * 创建用户评论
	 * 
	 * @param comment
	 * @return
	 * @throws MengException
	 */
	public boolean createComment(MyComment comment) throws MengException {

		if (UserInfo.getInstances().verifyUsername(comment.username)) {
			throw new MengException("140001", "用户名不存在");
		}

		if (!MyPosts.getInstances().verifyMyPosts(comment.posts_code)) {
			throw new MengException("140002", "用户帖子不存在");
		}

		try {
			comment.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createComment Exception," + t.getMessage());
			throw new MengException("140003", "用户评论保存失败");
		}
	}
	
	/**
	 * 修改用户评论
	 * @param comment
	 * @return
	 * @throws MengException
	 */
	public boolean modifyComment(MyComment comment) throws MengException {
		
		if (UserInfo.getInstances().verifyUsername(comment.username)) {
			throw new MengException("140004", "用户名不存在");
		}

		if (!MyPosts.getInstances().verifyMyPosts(comment.posts_code)) {
			throw new MengException("140005", "用户帖子不存在");
		}

		comment.id = getComment(comment.comment_code).id;
		
		try {
			comment.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyComment Exception," + t.getMessage());
			throw new MengException("140006", "用户评论修改失败");
		}
	}
	
	/**
	 * 删除用户评论
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public boolean destroyComment(String code) throws MengException {
		
		try {
			getComment(code).delete();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo destroyComment Exception," + t.getMessage());
			throw new MengException("140007", "删除用户评论失败");
		}
	}
	
	/**
	 * 获取用户评论列表
	 * @param username
	 * @param page
	 * @param size
	 * @return
	 * @throws MengException
	 */
	public List<MyComment> getCommentPage(String username, int page, int size) throws MengException {
		
		try {
			return find.where().eq("username", username).findPagingList(size).getPage(page).getList();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getCommentPage Exception," + t.getMessage());
			throw new MengException("140008", "获取用户评论列表失败");
		}
	}
	
	/**
	 * 获取用户评论
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public MyComment getComment(String code) throws MengException {
		
		try {
			return find.where().eq("username", username).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getComment Exception," + t.getMessage());
			throw new MengException("140009", "获取用户评论失败");
		}
	}
	
}
